package com.raylu.realtime.utils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;

/**
 * Description:
 * <p>
 * Create by lucienoz on 2022/1/4.
 * Copyright © 2022 lucienoz. All rights reserved.
 */
public class DateUtil {

    private static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static Long getEpochMilli(String dateTimeStr){
        LocalDateTime localDateTime = LocalDateTime.parse(dateTimeStr, dtf);
        long timestamp = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return timestamp;
    }

    public static String getFormatDateTime(long timestamp){
        String format = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()).format(dtf);
        return format;
    }

    public static String getFormatDateTime(LocalDateTime timestamp){
        String format = timestamp.format(dtf);
        return format;
    }

    public static LocalDateTime getLocalDateTime(Long timestamp){
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
        return localDateTime;
    }


    public static void main(String[] args) {

        LocalDateTime localDateTime = LocalDateTime.parse("2022-01-04 18:40:51", dtf);
        long toEpochMilli = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        System.out.println(toEpochMilli);

        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(toEpochMilli), ZoneId.systemDefault());
        String format = dateTime.format(dtf);

        System.out.println(format);


    }
}
